package thread;

/**
 *  StringBuilder不是并发安全的,StringBuffered是并发安全的
 */
public class SyncDemo4 {
    public static void main(String[] args) {
//        StringBuilder sb = new StringBuilder();
        StringBuffer sb = new StringBuffer();

        Thread t1 = new Thread(){
            public void run(){
                for(int i=0;i<1000;i++){
                    sb.append("abc");
                }
            }
        };
        Thread t2 = new Thread(){
            public void run(){
                for(int i=0;i<1000;i++){
                    sb.append("def");
                }
            }
        };
        t1.start();
        t2.start();

        try {
            Thread.sleep(1000);//让主线程阻塞一下,等待t1,t2线程拼接完毕
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(sb.length());


    }
}
